// 查看详情
function tscpDet(e) {
    var jgType = $('.link___9TVqH.select___4NqDw').attr('data-val');
    window.location.href = "product_details.html?jgType=" + jgType + "&productid=" + e
}

layui.use(['carousel', 'form', 'layer', 'dropdown'], function() {
    var layer = layui.layer;
    var form = layui.form;
    var carousel = layui.carousel;
	var dropdown = layui.dropdown;
    // 机构轮播展示定时器
    var timeInt = null;
    //机构名称hover显示tips
    var productNameTips;

    

    // 初始化数据
    initData();
	
	
    function initData() {
        showTotal();
        getProduct({
            url: 'mh/frontPage/getFrontPagePros4_bank'
        });
    };
    // 产品tab切换 
    $('.menu___1GDbd .link___9TVqH').click(function() {
        $(this).addClass('select___4NqDw').siblings().removeClass('select___4NqDw');
        // 筛选产品、修改金融机构title
        var t = $(this).text().trim();
        var v = $(this).attr('data-val');
        var url = "";
        if (t == '贷款类') {
            url = "mh/frontPage/getFrontPagePros4_bank";
            $('#jgContainer').text("银行机构");
        } else if (t == '保险类') {
            url = "mh/frontPage/getFrontPagePros4_insure"
            $('#jgContainer').text("保险机构");
        } else if (t == '担保类') {
            url = "mh/frontPage/getFrontPagePros4_db"
            $('#jgContainer').text("担保机构");
        } else {
            return
        }
        getProduct({
            t: t,
            url: url
        });
        // 展示轮播图
        if (v == '1') {
            clearInterval(timeInt)
            getOrgan({
                page: 1,
                rows: 15,
                code: 1
            });
        } else if (v == '2') {
            clearInterval(timeInt)
            getOrgan({
                page: 1,
                rows: 15,
                code: 2
            })
        }
    });


    // 查看更多
    $('.menu___1GDbd .query_more').click(function() {
        var code = $('.link___9TVqH.select___4NqDw').attr('data-val');
        window.location.href = "finance_product.html?code=" + code;
    });

    // 产品导航
    $('.tb_type .tb_body').click(function() {
        var fn = $(this).attr('fn');
        if (fn == '1' || fn == '2' || fn == '6') {
            window.location.href = "finance_product.html?code=" + fn;
        } else {
            layer.msg('正在建设中...', {
                icon: 4,
                time: 1500
            });
        }
    });

    // 投标保函入口
    $('#toToubiao').click(function() {
        var user = sessionStorage.getItem('baohan_info');
        if (!user) {
            window.open('http://113.238.142.204:8600/CSTPBidder/memberLogin');
        } else {
            window.location.href = "finance_letter.html"
        }
    });

    // 获取页面统计数据
    function showTotal() {
        $.ajax({
            type: 'post',
            url: sdk.baseIP + 'mh/frontPage/getFrontPageStatistics',
            success: function(d) {
                var status = d.status;
                var tData = d.data;
                // 展示数据
                if (status) {
                    $('.main #total1').text(tData.totalEnterpriseCount);
                    $('.main #total2').text(tData.totalEnterpriseMoney || 0);
                } else {
                    layer.msg('统计数据获取失败!', {
                        icon: 2,
                        closeBtn: 1,
                        time: 3000
                    });
                }
            }
        });
        $.ajax({
            type: 'get',
            url: sdk.baseIP + 'mh/click/record?source=1',
        });


        getOrgan({
            page: 1,
            rows: 15,
            code: 1
        })
    }

    // 获取机构产品(贷款、保险、担保、证券、基金)
    // 默认展示贷款类
    function getProduct(params) {
        var url = params.url;
        $.ajax({
            type: 'post',
            url: sdk.baseIP + url,
            success: function(d) {
				console.log('d',d,sdk.baseIP + url,)
                var status = d.status;
                var bankData = d.data;
                // 展示数据
                if (status) {
                    $('#listData').html($.templates('#bankDiv').render({
                        total: bankData.length,
                        bankList: bankData
                    }));
					productNameHover()
                } else {
                    layer.msg('数据获取失败!', {
                        icon: 2,
                        closeBtn: 1,
                        time: 3000
                    });
                }
            }
        })
    };


    // 金融机构及分页展示 
    function getOrgan(params) {
        var jgIns = null;
        $.ajax({
            type: 'post',
            url: sdk.baseIP + 'mh/jrjgPage/getGrid',
            data: {
                code: params.code,
                page: params.page,
                rows: params.rows,
            },
            dataType: "json",
            async: false, //使用同步的方式,true为异步方式
            success: function(d) {
                jgIns = d;
            }
        });
        if (jgIns.total > 0) {
            var y = jgIns.total % 15;
            var pages;
            var pageArr = [];
            if (y > 0) {
                pages = parseInt(jgIns.total / 15) + 1;
            } else {
                //pages = parseInt(jgIns.total / 15) / 15;
				pages = parseInt(jgIns.total / 15)
            }
            for (var i = 1; i <= pages; i++) {
                pageArr.push({
                    key: i
                });
            }
            $('#tabsLine').html($.templates('#tabScript').render({
                pages: pages,
                pageArr: pageArr
            }));
            //console.log('jgIns',jgIns,pages)
            $('#bankItem').html($.templates('#ckvpbeTmpl').render(jgIns));
            $('#tabsLine .tab[data-key=' + 1 + ']').addClass('tabBg'); // 默认第一个tab选中
            $('.tab').click(function() {
                var key = $(this).attr('data-key');
                $('#tabsLine .tab').removeClass('tabBg');
                $('#tabsLine .tab[data-key=' + key + ']').addClass('tabBg'); // 默认第一个tab选中
                $.ajax({
                    type: 'post',
                    url: sdk.baseIP + 'mh/jrjgPage/getGrid',
                    data: {
                        code: params.code,
                        page: key,
                        rows: params.rows,
                    },
                    dataType: "json",
                    success: function(d) {
                        $('#bankItem').html($.templates('#ckvpbeTmpl').render(d));

                    }
                });
            });

            // tab选中状态;
            var lineKey = $('#tabsLine .tab');
            var count = 1;
            timeInt = setInterval(function() {
                if (count == 1) {
                    count++
                } else if (count == pages) {
                    count = 1;
                } else {
                    count++
                }
                $('#tabsLine .tab').removeClass('tabBg');
                $('#tabsLine .tab[data-key=' + count + ']').addClass('tabBg');
                $.ajax({
                    type: 'post',
                    url: sdk.baseIP + 'mh/jrjgPage/getGrid',
                    data: {
                        code: params.code,
                        page: count,
                        rows: params.rows,
                    },
                    dataType: "json",
                    success: function(d) {
                        $('#bankItem').html($.templates('#ckvpbeTmpl').render(d));
                    }
                });
            }, 5000);


        } else {
            layer.msg('数据获取失败!', {
                icon: 2,
                closeBtn: 1,
                time: 3000
            });
        }
    };
	
	
	//机构hover展示
	function productNameHover() {
		$(".c_type").hover(function(e) {
			var that = this;
			productNameTips = layer.tips(`<span style='color:#000;'>${e.target.innerText}`,that,{tips:[1,'#fff'],time:0,area: 'auto',maxWidth:500});
		}, function() {
			layer.close(productNameTips);
		});
	}
	
	//贷款类选项卡 增加热门产品
	// dropdown.render({
	// 	elem: '.select___4NqDw',
	// 	trigger: 'hover',
	// 	align:'center',
	// 	className:'hotProduct',
	// 	delay:100,
	// 	data: [{
	// 		title:'热门产品',
	// 		id:'1',
	// 		href:'https://scf.qilerongrong.com.cn/login_new.jsp'
	// 	}]
	// })
});
